package com.sun.metric;

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

@ConditionalOnClass(CollectorRegistry.class)
@Component
public class PrometheusRegister {

    @Autowired
    public PrometheusRegister(ApplicationContext applicationContext, CollectorRegistry collectorRegistry) {
        Map<String, ? extends Collector> beanMap = applicationContext.getBeansOfType(Collector.class);
        beanMap.forEach((key, value) -> collectorRegistry.register(value));
    }

}
